<template>
  <el-dialog
    :title="!dataForm.newRoomId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="新房名称" prop="new Name">
      <el-input v-model="dataForm.new Name" placeholder="新房名称"></el-input>
    </el-form-item>
    <el-form-item label="新房标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="新房标题"></el-input>
    </el-form-item>
    <el-form-item label="单价" prop="onePrice">
      <el-input v-model="dataForm.onePrice" placeholder="单价"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="默认图片" prop="defaultImage">
      <el-input v-model="dataForm.defaultImage" placeholder="默认图片"></el-input>
    </el-form-item>
    <el-form-item label="楼层" prop="floor">
      <el-input v-model="dataForm.floor" placeholder="楼层"></el-input>
    </el-form-item>
    <el-form-item label="服务介绍" prop="introduce">
      <el-input v-model="dataForm.introduce" placeholder="服务介绍"></el-input>
    </el-form-item>
    <el-form-item label="核心买点" prop="soreSell">
      <el-input v-model="dataForm.soreSell" placeholder="核心买点"></el-input>
    </el-form-item>
    <el-form-item label="业主心态" prop="owner">
      <el-input v-model="dataForm.owner" placeholder="业主心态"></el-input>
    </el-form-item>
    <el-form-item label="1 商铺 2 写字楼 3 小区" prop="typeStatus">
      <el-input v-model="dataForm.typeStatus" placeholder="1 商铺 2 写字楼 3 小区"></el-input>
    </el-form-item>
    <el-form-item label="经纪人id" prop="brokerId">
      <el-input v-model="dataForm.brokerId" placeholder="经纪人id"></el-input>
    </el-form-item>
    <el-form-item label="属性组id" prop="propertyId">
      <el-input v-model="dataForm.propertyId" placeholder="属性组id"></el-input>
    </el-form-item>
    <el-form-item label="楼盘名称" prop="housesName">
      <el-input v-model="dataForm.housesName" placeholder="楼盘名称"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="roomAddress">
      <el-input v-model="dataForm.roomAddress" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="0 未提交 1 已提交 2 待提交" prop="status">
      <el-input v-model="dataForm.status" placeholder="0 未提交 1 已提交 2 待提交"></el-input>
    </el-form-item>
    <el-form-item label="地区id" prop="cityId">
      <el-input v-model="dataForm.cityId" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="是否同步 es(1 或 0 )" prop="isEs">
      <el-input v-model="dataForm.isEs" placeholder="是否同步 es(1 或 0 )"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除" prop="delStatus">
      <el-input v-model="dataForm.delStatus" placeholder="是否删除"></el-input>
    </el-form-item>
    <el-form-item label="房子 0 新房 1 二手房" prop="houseStatus">
      <el-input v-model="dataForm.houseStatus" placeholder="房子 0 新房 1 二手房"></el-input>
    </el-form-item>
    <el-form-item label="房源类型id" prop="houseTypeId">
      <el-input v-model="dataForm.houseTypeId" placeholder="房源类型id"></el-input>
    </el-form-item>
    <el-form-item label="房源图片id" prop="roomImgId">
      <el-input v-model="dataForm.roomImgId" placeholder="房源图片id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          newRoomId: 0,
          new Name: '',
          title: '',
          onePrice: '',
          price: '',
          area: '',
          defaultImage: '',
          floor: '',
          introduce: '',
          soreSell: '',
          owner: '',
          typeStatus: '',
          brokerId: '',
          propertyId: '',
          housesName: '',
          roomAddress: '',
          sort: '',
          status: '',
          cityId: '',
          isEs: '',
          createTime: '',
          updateTime: '',
          delStatus: '',
          houseStatus: '',
          houseTypeId: '',
          roomImgId: ''
        },
        dataRule: {
          new Name: [
            { required: true, message: '新房名称不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '新房标题不能为空', trigger: 'blur' }
          ],
          onePrice: [
            { required: true, message: '单价不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          defaultImage: [
            { required: true, message: '默认图片不能为空', trigger: 'blur' }
          ],
          floor: [
            { required: true, message: '楼层不能为空', trigger: 'blur' }
          ],
          introduce: [
            { required: true, message: '服务介绍不能为空', trigger: 'blur' }
          ],
          soreSell: [
            { required: true, message: '核心买点不能为空', trigger: 'blur' }
          ],
          owner: [
            { required: true, message: '业主心态不能为空', trigger: 'blur' }
          ],
          typeStatus: [
            { required: true, message: '1 商铺 2 写字楼 3 小区不能为空', trigger: 'blur' }
          ],
          brokerId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ],
          propertyId: [
            { required: true, message: '属性组id不能为空', trigger: 'blur' }
          ],
          housesName: [
            { required: true, message: '楼盘名称不能为空', trigger: 'blur' }
          ],
          roomAddress: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '0 未提交 1 已提交 2 待提交不能为空', trigger: 'blur' }
          ],
          cityId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          isEs: [
            { required: true, message: '是否同步 es(1 或 0 )不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          delStatus: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ],
          houseStatus: [
            { required: true, message: '房子 0 新房 1 二手房不能为空', trigger: 'blur' }
          ],
          houseTypeId: [
            { required: true, message: '房源类型id不能为空', trigger: 'blur' }
          ],
          roomImgId: [
            { required: true, message: '房源图片id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.newRoomId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.newRoomId) {
            this.$http({
              url: this.$http.adornUrl(`/house/househouse/info/${this.dataForm.newRoomId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.new Name = data.houseHouse.new Name
                this.dataForm.title = data.houseHouse.title
                this.dataForm.onePrice = data.houseHouse.onePrice
                this.dataForm.price = data.houseHouse.price
                this.dataForm.area = data.houseHouse.area
                this.dataForm.defaultImage = data.houseHouse.defaultImage
                this.dataForm.floor = data.houseHouse.floor
                this.dataForm.introduce = data.houseHouse.introduce
                this.dataForm.soreSell = data.houseHouse.soreSell
                this.dataForm.owner = data.houseHouse.owner
                this.dataForm.typeStatus = data.houseHouse.typeStatus
                this.dataForm.brokerId = data.houseHouse.brokerId
                this.dataForm.propertyId = data.houseHouse.propertyId
                this.dataForm.housesName = data.houseHouse.housesName
                this.dataForm.roomAddress = data.houseHouse.roomAddress
                this.dataForm.sort = data.houseHouse.sort
                this.dataForm.status = data.houseHouse.status
                this.dataForm.cityId = data.houseHouse.cityId
                this.dataForm.isEs = data.houseHouse.isEs
                this.dataForm.createTime = data.houseHouse.createTime
                this.dataForm.updateTime = data.houseHouse.updateTime
                this.dataForm.delStatus = data.houseHouse.delStatus
                this.dataForm.houseStatus = data.houseHouse.houseStatus
                this.dataForm.houseTypeId = data.houseHouse.houseTypeId
                this.dataForm.roomImgId = data.houseHouse.roomImgId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/househouse/${!this.dataForm.newRoomId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'newRoomId': this.dataForm.newRoomId || undefined,
                'new Name': this.dataForm.new Name,
                'title': this.dataForm.title,
                'onePrice': this.dataForm.onePrice,
                'price': this.dataForm.price,
                'area': this.dataForm.area,
                'defaultImage': this.dataForm.defaultImage,
                'floor': this.dataForm.floor,
                'introduce': this.dataForm.introduce,
                'soreSell': this.dataForm.soreSell,
                'owner': this.dataForm.owner,
                'typeStatus': this.dataForm.typeStatus,
                'brokerId': this.dataForm.brokerId,
                'propertyId': this.dataForm.propertyId,
                'housesName': this.dataForm.housesName,
                'roomAddress': this.dataForm.roomAddress,
                'sort': this.dataForm.sort,
                'status': this.dataForm.status,
                'cityId': this.dataForm.cityId,
                'isEs': this.dataForm.isEs,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'delStatus': this.dataForm.delStatus,
                'houseStatus': this.dataForm.houseStatus,
                'houseTypeId': this.dataForm.houseTypeId,
                'roomImgId': this.dataForm.roomImgId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
